package org.example.factory.support;

import org.example.context.ApplicationContext;
import org.example.context.ApplicationContextAware;
import org.example.exception.BeansException;
import org.example.factory.config.BeanPostProcessor;

/**
 * @Author: luyg
 * @Date: 2024/09/10/12:12
 * @Description:
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {
    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    @Override
    public Object beanProcessorBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof ApplicationContextAware){
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object beanProcessorAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
